//
//  LDXVisitorView.swift
//  微博
//
//  Created by 吕东星 on 17/3/30.
//  Copyright © 2017年 lddongxing. All rights reserved.
//

import UIKit

import SnapKit
protocol LDXVisitorViewDelegate: NSObjectProtocol {
    func loginOpration()
    
    
}

class LDXVisitorView: UIView {
    
//    旋转视图
   private lazy var cycleImageView: UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    //罩层
   private lazy var coverImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //主页
   private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
   private lazy var messageLable: UILabel = {
       let lable = UILabel()
        lable.font = UIFont.systemFont(ofSize: 14)
        lable.textColor = UIColor.darkGray
        lable.textAlignment = .center
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.numberOfLines = 0
      return lable
    }()
    
    private lazy var registerButton: UIButton = {
    let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  手写代码拉伸
        //        UIImage().resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false

        
        
     return button
    }()
    //  登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self,action: #selector(loginAction), for:.touchUpInside)
//        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  手写代码拉伸
        
        //        UIImage().resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    // 设置代理对象  (弱引用)
   weak var delegate: LDXVisitorViewDelegate?
    // 设置闭包
    var loginClosure:(()->())?
    
    //自定义视图重写方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

   private func setupUI()  {
    backgroundColor = UIColor(white: 237.0/255, alpha: 1)
    // 添加视图
    addSubview(cycleImageView)
    addSubview(coverImageView)
    addSubview(homeImageView)
    addSubview(messageLable)
    addSubview(registerButton)
    addSubview(loginButton)
    // 自动布局
    cycleImageView.snp.makeConstraints { (make) in
        make.center.equalTo(self)
    }
    coverImageView.snp.makeConstraints { (make) in
        make.center.equalTo(cycleImageView)
    }
    homeImageView.snp.makeConstraints { (make) in
        make.center.equalTo(coverImageView)
    }
    messageLable.snp.makeConstraints { (make) in
        make.centerX.equalTo(homeImageView)
        make.top.equalTo(cycleImageView.snp.bottom)
        make.width.equalTo(224)
    }
    registerButton.snp.makeConstraints { (make) in
        make.leading.equalTo(messageLable)
        make.top.equalTo(messageLable.snp.bottom).offset(10)
        make.size.equalTo(CGSize(width: 80, height: 35))
    }
    
    loginButton.snp.makeConstraints { (make) in
        make.trailing.equalTo(messageLable)
        make.top.equalTo(registerButton)
        make.size.equalTo(registerButton)
    }
    
    // 手动代码自动布局
    // 关闭translatesAutoresizingMaskIntoConstraints
//    cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//    
//    addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy:.equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy:.equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
//    
    
}
    
   private func startAnimation()  {
        //  创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  目的地
        animation.toValue = 2 * M_PI
        //  指定动画时长
        animation.duration = 20
        //  动画的执行次数, MAXFLOAT最大值
        animation.repeatCount = MAXFLOAT
        //  动画失去焦点,动画会释放, 设置false在动画执行次数内不会释放动画对象
        animation.isRemovedOnCompletion = false
        //执行动画
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    
    // 修改访客视图信息
    func updateVisitor(imageName: String?,message: String?)  {
        if let imgName = imageName ,let msg = message {
            homeImageView.image = UIImage(named: imgName)
            messageLable.text = msg
            cycleImageView.isHidden = true
            
        }else{
            
              startAnimation()
        }
        
    }
    // 登录按钮跳转
       @objc private func loginAction()  {
        //代理方法
//        delegate?.loginOpration()
        //闭包方法
        loginClosure?()
    }
    
    
    
    
}
